home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / QuickDraw GX Aware Sample ƒ / Simple Sample GX ƒ / events.c next >
Encoding:
C/C++ Source or Header  |  1996-03-20  |  10.1 KB  |  427 lines  |  [TEXT/MMCC]

  1. /*********************************************************************
  2.  
  3.     events.c
  4.     
  5.     This file contains the event handling code for the QuickDraw GX
  6.     aware sample, "Simple Sample GX."
  7.     
  8.     Additional info can be found in the related develop #19 article,
  9.     "Adding QuickDraw GX Printing to QuickDraw Applications."
  10.  
  11.     Dave Hersey, Apple Developer Technical Support.
  12.     
  13.     ——————— Edit Trail ———————
  14.     
  15.     begat:                                            1/22/94  - dmh
  16.     cleaned up for 2nd draft of develop article:    3/10/94  - dmh
  17.     cleaned up for final:                            4/14/94  - dmh
  18.     added NewAEEventHandlerProc to build ppc upp    8/16/94  - nick
  19.     
  20. *********************************************************************/
  21.  
  22. #include "Simple Sample.h"
  23.  
  24.  
  25. /************************************************************
  26.   MyEventLoop - This routine (in conjunction with main)
  27.   makes up our event loop.
  28.  
  29.  *************************************************************/
  30.  
  31. void MyEventLoop()
  32. {
  33.     EventRecord        theEvent;
  34.  
  35.     if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
  36.         MyDoEvent(&theEvent);
  37. }
  38.  
  39.  
  40. /************************************************************
  41.   MyDoEvent - This routine handles event dispatching.
  42.  
  43.  *************************************************************/
  44.  
  45. void MyDoEvent(EventRecord *theEvent)
  46. {
  47.     char            key;
  48.     WindowPtr         whichWindow;
  49.     MyDocumentPtr    windowDoc;
  50.  
  51.     switch(theEvent->what)
  52.     {                    
  53.         case updateEvt:            // Updating a window.
  54.  
  55.             if (((WindowPeek) theEvent->message)->windowKind == userKind)
  56.                 MyUpdateWindow((WindowPtr) theEvent->message);
  57.  
  58.             break;
  59.         
  60.         case mouseDown:            // Mousedown in a window or menu.
  61.     
  62.             switch (FindWindow(theEvent->where, &whichWindow))
  63.             {
  64.                 case inSysWindow:
  65.  
  66.                     SystemClick(theEvent, whichWindow);
  67.                     break;
  68.                         
  69.                 case inDrag:
  70.  
  71.                     if (((WindowPeek) whichWindow)->windowKind == userKind)
  72.                     {
  73.                         DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  74.                         MyAdjustMenus();
  75.                     }
  76.                     break;
  77.  
  78.                 case inGoAway:
  79.  
  80.                     if ((((WindowPeek) whichWindow)->windowKind == userKind) &&
  81.                         (TrackGoAway(whichWindow, theEvent->where)))
  82.                     {
  83.                         MyDisposeDocument(MyGetDocPtr(whichWindow));
  84.                         MyAdjustMenus();
  85.                     }
  86.                     
  87.                     break;
  88.                 
  89.                 case inContent:
  90.  
  91.                     if (((WindowPeek) whichWindow)->windowKind == userKind)
  92.                     {
  93.                         SelectWindow(whichWindow);
  94.                         MyAdjustMenus();
  95.                     }
  96.                     
  97.                     break;
  98.  
  99.                 case inMenuBar:
  100.                     MyDoMenuCommand(MenuSelect(theEvent->where));
  101.                     MyAdjustMenus();
  102.                     break;
  103.             }
  104.  
  105.         case keyDown:            // Key pressed.
  106.         case autoKey:
  107.  
  108.             key = theEvent->message & charCodeMask;
  109.             if ((theEvent->modifiers & cmdKey) &&
  110.                 (theEvent->what == keyDown))
  111.                 {
  112.                     MyDoMenuCommand(MenuKey(key));
  113.                     MyAdjustMenus();
  114.                 }
  115.             
  116.             break;
  117.  
  118.         case kOSEvent:            // Operating system event.
  119.  
  120.             switch ((unsigned long) theEvent->message >> 24)
  121.             {
  122.                  case kSuspendResumeMessage:                            // Suspend event:
  123.                     if ((theEvent->message & kResumeMask) == 0)
  124.                         gSleep = 80;
  125.                     else                                            // Resume event:
  126.                     {
  127.                         gSleep = 0;
  128.                         SetCursor(&qd.arrow);
  129.  
  130. /*
  131.     If we're using QuickDraw GX, we need to call GXUpdateJob on all of
  132.     our gxJob objects whenever we receive a resume event.  Otherwise,
  133.     new user settings might not be reflected in these jobs.
  134. */
  135.                         if (gGXIsPresent)
  136.                         {
  137.                             whichWindow = FrontWindow();
  138.                         
  139.                             while (whichWindow != nil)
  140.                             {
  141.                                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  142.                                 {
  143.                                     windowDoc = MyGetDocPtr(whichWindow);
  144.                                     GXUpdateJob(windowDoc->documentJob);
  145.                                 }
  146.                                 
  147.                                 whichWindow = (WindowPtr) ((WindowPeek) whichWindow)->nextWindow;
  148.                             }
  149.                         }
  150.                     }
  151.  
  152.                 break;
  153.             }
  154.  
  155.             break;
  156.                 
  157.         case kHighLevelEvent:    // AppleEvent.
  158.  
  159.             AEProcessAppleEvent(theEvent);
  160.             break;
  161.     }
  162. }
  163.  
  164.  
  165. /************************************************************
  166.   MyDoAEInstallation - This routine installs our core
  167.   AppleEvent handlers.
  168.  
  169. *************************************************************/
  170.  
  171. void MyDoAEInstallation()
  172. {
  173.     AEInstallEventHandler(kCoreEventClass,
  174.                           kAEOpenApplication,
  175.                           NewAEEventHandlerProc(MyHandleOAPP),
  176.                           0, false);
  177.  
  178.     AEInstallEventHandler(kCoreEventClass,
  179.                           kAEQuitApplication,
  180.                           NewAEEventHandlerProc(MyHandleQUIT),
  181.                           0, false);
  182.  
  183.     AEInstallEventHandler(kCoreEventClass,
  184.                           kAEOpenDocuments,
  185.                           NewAEEventHandlerProc(MyHandleODOC),
  186.                           0, false);
  187.  
  188.     AEInstallEventHandler(kCoreEventClass,
  189.                           kAEPrintDocuments,
  190.                           NewAEEventHandlerProc(MyHandlePDOC),
  191.                           0, false);
  192. }
  193.  
  194.  
  195. /************************************************************
  196.   MyHandleOAPP - This is the "open application" AppleEvent
  197.   handler.  We check out the message, and set our "should we
  198.   quit after handling a 'pdoc'" flag to false.  If we got an
  199.   'oapp', we weren't launched via 'pdoc'.
  200.  
  201. *************************************************************/
  202.  
  203. pascal OSErr MyHandleOAPP(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  204. {
  205. #pragma unused (reply, myRefCon)
  206.  
  207.     gQuitAfterPrinting = false;
  208.     return MyCheckAEParams(theAppleEvent);
  209. }
  210.  
  211.  
  212. /************************************************************
  213.   MyHandleQUIT - This is the "quit" AppleEvent handler.  Set
  214.   the gQuitting flag and the event loop will do the rest.
  215.  
  216. *************************************************************/
  217.  
  218. pascal OSErr MyHandleQUIT(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  219. {
  220. #pragma unused (reply, myRefCon)
  221.  
  222.     gQuitting = true;
  223.     return MyCheckAEParams(theAppleEvent);
  224. }
  225.  
  226.  
  227. /************************************************************
  228.   MyHandleODOC - This is the "open document" AppleEvent
  229.   handler, so open some documents.
  230.  
  231. *************************************************************/
  232.  
  233. pascal OSErr MyHandleODOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  234. {
  235.     OSErr            err;
  236.     AEDescList        docList;
  237.     FSSpec            myFSS;
  238.     AEKeyword        theKeyword;
  239.     DescType        typeCode;
  240.     long            doc, itemsInList;
  241.     Size            actualSize;
  242.     MyDocumentPtr    newDocument;
  243.  
  244. #pragma unused (reply, myRefCon)
  245.  
  246.     gQuitAfterPrinting = false;
  247.  
  248. // Get the event's document list.
  249.  
  250.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  251.     nrequire(err, AEError);
  252.  
  253.     err = MyCheckAEParams(theAppleEvent);
  254.     nrequire(err, CheckAEParamsFailed);
  255.     
  256.  
  257. // Open all entries in the document list.
  258.  
  259.     err = AECountItems(&docList, &itemsInList);
  260.     nrequire(err, AECountItemsFailed);
  261.  
  262.     for (doc = 1; (!err) && (doc <= itemsInList); doc++)
  263.     {
  264.         err = AEGetNthPtr(&docList, doc, typeFSS, &theKeyword, &typeCode,
  265.                           (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
  266.     
  267.         nrequire(err, AEEntryError);
  268.  
  269.         err = MyCreateDocument(kDefaultTitle, &newDocument);
  270.         nrequire(err, CreateDocFailed);
  271.  
  272.         err = MyFSLoadDocument(newDocument, &myFSS, false);
  273.  
  274.         if (err == noErr)
  275.         {
  276.             ShowWindow(newDocument->documentWindow);
  277.             SelectWindow(newDocument->documentWindow);
  278.         }
  279.         else
  280.             MyDisposeDocument(newDocument);
  281.     }
  282.  
  283. CreateDocFailed:
  284. AEEntryError:
  285. AECountItemsFailed:
  286. CheckAEParamsFailed:
  287.     AEDisposeDesc(&docList);
  288.     MyAdjustMenus();
  289.  
  290. AEError:
  291.     return err;
  292. }
  293.  
  294.  
  295. /************************************************************
  296.   MyHandlePDOC - This is the "print document" AppleEvent
  297.   handler, so print some documents.
  298.  
  299. *************************************************************/
  300.  
  301. pascal OSErr MyHandlePDOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  302. {
  303.     OSErr            err;
  304.     AEDescList        docList, dtpList;
  305.     FSSpec            myFSS, dtpFSS;
  306.     long            itemsInList, i;
  307.     AEKeyword        theKeyword;
  308.     DescType        typeCode;
  309.     Boolean            draggedToDTP = false;
  310.     Size            actualSize;
  311.     MyDocumentPtr    newDocument;
  312.  
  313. #pragma unused (reply, myRefCon)
  314.  
  315. // See if the document was dragged to a desktop printer.
  316.  
  317.     err = AEGetAttributeDesc(theAppleEvent, keyOptionalKeywordAttr, typeAEList, &dtpList);
  318.     if (err == noErr) draggedToDTP = true;
  319.  
  320. /*
  321.     If we dragged to a desktop printer, get the name
  322.     of the desktop printer and then throw away the
  323.     description list for it.
  324. */
  325.  
  326.     if (draggedToDTP)
  327.     {
  328.         err = AEGetNthPtr(&dtpList, 1, typeFSS, &theKeyword, &typeCode,
  329.                           (Ptr) &dtpFSS, sizeof(FSSpec), &actualSize);
  330.     
  331.         AEDisposeDesc(&dtpList);
  332.     }
  333.  
  334. // Get our document list.
  335.  
  336.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  337.     nrequire(err, AEError);
  338.  
  339. /*
  340.     Make sure we've accounted for all of the parameters
  341.     passed, and count the number of documents passed in.
  342. */
  343.  
  344.     err = MyCheckAEParams(theAppleEvent);
  345.     nrequire(err, AEError);
  346.     
  347.     err = AECountItems(&docList, &itemsInList);
  348.     nrequire(err, AEError);
  349.  
  350. /*
  351.     For each entry in the document list, load it,
  352.     print it, and close it.
  353. */
  354.  
  355.     for (i = 1; i<= itemsInList; i++)
  356.     {
  357.         err = AEGetNthPtr(&docList, i, typeFSS, &theKeyword, &typeCode,
  358.                           (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
  359.  
  360.         nrequire(err, AEEntryError);
  361.  
  362. // Load the document.
  363.  
  364.         err = MyCreateDocument(kDefaultTitle, &newDocument);
  365.         nrequire(err, CreateDocFailed);
  366.  
  367.         err = MyFSLoadDocument(newDocument, &myFSS, true);
  368.         nrequire(err, LoadDocFailed);
  369.  
  370. /*
  371.     If we dragged to a desktop printer, select that as the
  372.     output printer for each job and do a "Print One Copy."
  373.     Otherwise, we're just doing a "Print" from the Finder,
  374.     so print with dialogs.
  375. */
  376.  
  377.         if (draggedToDTP)
  378.         {
  379.             GXSelectJobOutputPrinter(newDocument->documentJob, dtpFSS.name);
  380.             err = MyPrintOneCopy(newDocument);
  381.         }
  382.         else
  383.             err = MyPrintDocument(newDocument);
  384.  
  385. // Close the document once it's printed.
  386.  
  387. LoadDocFailed:
  388.         MyDisposeDocument(newDocument);
  389.     }
  390.  
  391. // When we're all done throw away the description lists and return.
  392.  
  393. CreateDocFailed:
  394. AEEntryError:
  395.         AEDisposeDesc(&docList);
  396.  
  397. AEError:
  398.     if (gQuitAfterPrinting)
  399.         gQuitting = true;
  400.  
  401.     return err;
  402. }
  403.  
  404.  
  405. /************************************************************
  406.   MyCheckAEParams - This routine is used to make sure we've
  407.   received all the parameters for an AppleEvent.  If so, we
  408.   return noErr, otherwise we indicate that the event wasn't
  409.   handled completely.
  410.  
  411.  *************************************************************/
  412.  
  413. OSErr MyCheckAEParams(AppleEvent *theAppleEvent)
  414. {
  415.     OSErr        err;
  416.     DescType    typeCode;
  417.     Size        actualSize;
  418.     
  419.     err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  420.                             typeWildCard, &typeCode, nil, 0, &actualSize);
  421.  
  422.     if (err == errAEDescNotFound) return noErr;
  423.     if (err == noErr) return errAEEventNotHandled;
  424.     
  425.     return err;
  426. }
  427.